package com.example.service.impl;

import com.example.mapper.StudentMapper;
import com.example.pojo.PageResult;
import com.example.pojo.Student;
import com.example.pojo.StudentQueryParam;
import com.example.service.StudentService;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;

/**
 * @Auther: Sun
 * @Date: 2025/9/22 - 09 - 22 - 18:49
 * @Description: com.example.service.impl
 */
@Service
public class StudentServiceImpl implements StudentService {
    @Autowired private StudentMapper studentMapper;


    @Override
    public PageResult<Student> list(StudentQueryParam studentQueryParam) {
         PageHelper.startPage(studentQueryParam.getPage(),studentQueryParam.getPageSize());
         List<Student> studentPage = studentMapper.list(studentQueryParam);
         Page<Student> page = (Page<Student>) studentPage;
         return new PageResult<Student>(page.getTotal(),page.getResult());
    }

    @Override
    public void violation(Integer id, Integer score) {
        if(score == null||score<=0){
            throw new RuntimeException("扣分不能为空和小于零");
        }
        Student student = studentMapper.getById(id);
        if(student == null){
            throw new RuntimeException("学生不存在");

        }
        student.setViolationCount(student.getViolationCount()+1);
        student.setViolationScore(student.getViolationScore()+score);
        studentMapper.updateById(student);

    }

    @Override
    public void updateById(Student student) {
        student.setUpdateTime(LocalDateTime.now());
        studentMapper.updateById(student);
    }

    @Override
    public Student getById(Integer id) {
        return studentMapper.getById(id);
    }

    @Override
    public void insert(Student student) {
        student.setViolationCount(0);
        student.setViolationScore(0);
        student.setCreateTime(LocalDateTime.now());
        student.setUpdateTime(LocalDateTime.now());
        studentMapper.insert(student);
    }

    @Override
    public void delete(List<Integer> ids) {
        studentMapper.delete(ids);
    }
}
